package cn.syncrond.tcm;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import cn.syncrond.tcm.core.definition.ITask;
import cn.syncrond.tcm.core.definition.ITaskExceptionResolver;
import cn.syncrond.tcm.core.pool.TaskPool;
import cn.syncrond.tcm.core.pool.TaskThreadPoolExecutor;
import cn.syncrond.tcm.service.log.ILogService;
import cn.syncrond.tcm.service.log.SimpleLogService;
import cn.syncrond.tcm.util.Reflections;
import cn.syncrond.tcm.web.rmi.RmiServer;

/**
 * @Description: 应用上下文，用于注入外部文件的配置
 * 	从外部配置文件加载用户配置，并注入到其他对象
 * @author meepai
 * @date 2016-11-21
 */
public class AppContext {

	private static AppContext instance;
	
	/**
	 * 配置项
	 */
	private Properties properties;
	
	private AppContext(){
		loadProperties();
	}
	
	/**
	 * 单例入口
	 * @return
	 */
	public static final AppContext getInstance(){
		if(instance == null){
			instance = new AppContext();
		}
		return instance;
	}
	
	/**
	 * 加载配置文件  
	 */
	private void loadProperties(){
		properties = new Properties();
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("tcm.properties");
		if(in != null){
			try {
				properties.load(in);
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 注入任务的配置属性
	 * @param task
	 */
	public void injectTask(ITask task){
		
	}
	
	/**
	 * 注入指定对象的属性
	 */
	public void injectTaskThreadPoolExecutor(TaskThreadPoolExecutor executor){
		// 反射日志服务
		String logServiceClass = properties.getProperty(ILogService.class.getName(), SimpleLogService.class.getName());
		if(StringUtils.isNotBlank(logServiceClass)){
			ILogService logService = Reflections.newInstance(logServiceClass, ILogService.class);
			executor.setLogService(logService);
		}
		
		// 反射异常处理
		String exceptionResolverClass = properties.getProperty(ITaskExceptionResolver.class.getName());
		if(StringUtils.isNotBlank(exceptionResolverClass)){
			ITaskExceptionResolver exceptionResolver = Reflections.newInstance(exceptionResolverClass, ITaskExceptionResolver.class);
			executor.setExceptionResolver(exceptionResolver);
		}
		
		// 任务列表的上限值
		String currentTaskUpperLimit = properties.getProperty(TaskThreadPoolExecutor.class.getName() + ".currentTaskUpperLimit", "100");
		if(StringUtils.isNotBlank(currentTaskUpperLimit)){
			try {
				executor.setCurrentTaskUpperLimit(Integer.parseInt(currentTaskUpperLimit.trim()));
			} catch(Exception e){}
		}
		String errorTaskUpperLimit = properties.getProperty(TaskThreadPoolExecutor.class.getName() + ".errorTaskUpperLimit", "100");
		if(StringUtils.isNotBlank(errorTaskUpperLimit)){
			try {
				executor.setErrorTaskUpperLimit(Integer.parseInt(errorTaskUpperLimit.trim()));
			} catch(Exception e){}
		}
		String completeTaskUpperLimit = properties.getProperty(TaskThreadPoolExecutor.class.getName() + ".completeTaskUpperLimit", "4000");
		if(StringUtils.isNotBlank(errorTaskUpperLimit)){
			try {
				executor.setCompleteTaskUpperLimit(Integer.parseInt(completeTaskUpperLimit.trim()));
			} catch(Exception e){}
		}
		String cancelTaskUpperLimit = properties.getProperty(TaskThreadPoolExecutor.class.getName() + ".cancelTaskUpperLimit", "100");
		if(StringUtils.isNotBlank(cancelTaskUpperLimit)){
			try {
				executor.setCompleteTaskUpperLimit(Integer.parseInt(cancelTaskUpperLimit.trim()));
			} catch(Exception e){}
		}
	}
	
	/**
	 * 获取初始的线程池大小
	 * @return
	 */
	public int getInitThreadPoolSize() {
		String poolSize = properties.getProperty(TaskPool.class.getName() + ".poolSize", "100");
		if(StringUtils.isNoneBlank(poolSize)){
			try {
				return Integer.parseInt(poolSize.trim());
			} catch(Exception e){}
		}
		return 100;
	}
	
	/**
	 * 获取初始化的服务端口
	 * @return
	 */
	public String getInitRmiServerPort(){
		return properties.getProperty(RmiServer.class.getName() + ".port");
	}
	
	/**
	 * 获取初始化的服务地址
	 * @return
	 */
	public String getInitRmiServiceUrl(){
		return properties.getProperty(RmiServer.class.getName() + ".url");
	}
}
